package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.enums.BFeeModelEnum;
import cn.huiyunche.base.service.enums.BFeeTypeEnum;
import cn.huiyunche.base.service.interfaces.BFeeModelService;
import cn.huiyunche.base.service.interfaces.BFeeTypeService;
import cn.huiyunche.base.service.interfaces.STrailerFeeService;
import cn.huiyunche.base.service.mappers.STrailerFeeMapper;
import cn.huiyunche.base.service.model.BFeeType;
import cn.huiyunche.base.service.model.STrailerFee;
import cn.huiyunche.base.service.model.STrailerFeeExample;
import cn.huiyunche.base.service.vo.SOrderFeeDetailVo;
import cn.huiyunche.tools.basic.exceptions.BusinessException;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @FileName: cn.huiyunche.app.service.interfaces.impl
 * @Description: Description
 * @author: Aaron
 * @date: 2016/11/16 下午4:58
 */
@Service
public class STrailerFeeServiceImpl implements STrailerFeeService {

    private static final Logger LOGGER = LoggerFactory.getLogger(STrailerFeeServiceImpl.class);

    @Autowired
    private BFeeModelService bFeeModelService;

    @Autowired
    private BFeeTypeService bFeeTypeService;

    @Autowired
    private STrailerFeeMapper sTrailerFeeMapper;

    @Override
    public SOrderFeeDetailVo queryTrailerFee(String areaCode, Integer trailerId) {
        LOGGER.info("queryTrailerFee param : {}", trailerId);
        if (StringUtils.isBlank(areaCode)) {
            LOGGER.error("queryTrailerFee param areaCode must not be null");
            throw new IllegalArgumentException("区域编码不能为空");
        }
        if (null == trailerId) {
            LOGGER.error("queryTrailerFee param trailerId must not be null");
            throw new IllegalArgumentException("拖车类型主键不能为空");
        }

        //查询费用类型信息
        BFeeType feeType = bFeeTypeService.getByCode(bFeeModelService.getByCode(BFeeModelEnum.CITYWIDE_ORDER).getId(), Arrays.asList(BFeeTypeEnum.TRAILER.getValue())).get(0);
        SOrderFeeDetailVo vo = new SOrderFeeDetailVo();
        vo.setId(feeType.getId());
        vo.setFeeCode(feeType.getFeeCode());
        vo.setFeeName(feeType.getFeeName());

        STrailerFeeExample example = new STrailerFeeExample();
        example.createCriteria().andBTrailerTypeIdEqualTo(trailerId).andBAreaCodeEqualTo(areaCode);
        List<STrailerFee> list = sTrailerFeeMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list)) {
            LOGGER.error("queryTrailerFee list must not be empty");
            throw new BusinessException("查询拖车使用费失败");
        }

        vo.setCost(list.get(0).getUsageFee());
        vo.setActualAmount(vo.getCost());

        return vo;
    }
}
